package cn.edu.njuit.webserver.securityserver.orders;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @Description: TODO
 * @Data: 2022/9/19 17:17
 * @Author: guzy
 */
@Service
public class UserOrderServiceImpl implements UserOrderService {
    private Log logger = LogFactory.getLog("用户数据服务");
    private final OrderRepository orderRepository;

    @Resource
    OrderQueueService orderQueueService;

    public UserOrderServiceImpl(OrderRepository orderRepository) {
        this.orderRepository = orderRepository;
    }

    /**
     * 队列4消息消费者：用户完成订单
     *
     * @param message
     */
    @RabbitListener(queues = "queue.order.complete")
    public void listenShopDelive(String message) {
        logger.info(String.format("收到配送完成消息，订单完成：%s", message));
        //用户收到配送完成的订单，自动完成订单
        OrderEntity orderEntity = this.finishOrder(message);
    }


    /**
     * 生成新的订单号
     *
     * @return
     */
    private String getNewOrderCode() {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("DDyyyyMMHHmmssSS");
        String timeString = simpleDateFormat.format(new Date());
        return timeString;
    }

    @Override
    public OrderEntity createOrder(String goodsName, Long goodsCount, Long goodsPrice, Long totalPrice, String user) {
        this.logger.info(String.format("创建新订单：%s", user));
        OrderEntity orderEntity = new OrderEntity();
        //获取订单号
        orderEntity.setOrderCode(this.getNewOrderCode());
        orderEntity.setOrderUser(user);
        orderEntity.setGoodsCount(goodsCount);
        orderEntity.setOrderTime(LocalDateTime.now());
        orderEntity.setOrderStatus(OrderStatus.INIT.getName());
        orderEntity.setGoodsName(goodsName);
        orderEntity.setOrderTotalPrice(totalPrice);
        this.logger.info(String.format("订单创建完成：%s", user));
        orderEntity = this.orderRepository.save(orderEntity);

        //添加以下代码，使用消息操作类完成消息推送：向队列1发送消息
        this.orderQueueService.sendCreateMessage(orderEntity.getOrderCode());

        return orderEntity;
    }

    @Override
    public List<OrderEntity> getMyOrders(String user) {
        return this.orderRepository.findByOrderUserOrderByOrderTimeDesc(user);
    }

    @Override
    public OrderEntity finishOrder(String orderCode) {
        OrderEntity orderEntity = this.orderRepository.findByOrderCode(orderCode);
        if (orderEntity != null) {
            orderEntity.setOrderStatus(OrderStatus.COMPLETE.getName());
            orderEntity.setOrderCompleteTime(LocalDateTime.now());
            //保存订单数据
            orderEntity = this.orderRepository.save(orderEntity);
            this.logger.info(String.format("订单完成：%s", orderCode));
            return orderEntity;
        }
        return null;
    }
}
